﻿using System.Collections.Generic;
using System.Linq;

namespace Mbs.Windows
{
    public class Workspace
    {
        public static IWorkspace Get(string workspaceName)
        {
            return ExtensionLocator.Get<IWorkspace>(workspaceName);
        }
    }

    public class ExtensionLocator
    {
        private static Dictionary<string, object> Map;
        static ExtensionLocator()
        {
            Map = new Dictionary<string, object>();
        }

        public static void Register(string name, object o)
        {
            lock(Map)
                Map.Add(name, o);
        }

        public static TService Get<TService>(string name)
        {
            object result = null;
            lock(Map)
                Map.TryGetValue(name, out result);
            return (TService) result;
        }

        public static bool ContainsObject(object o)
        {
            return Map.ContainsValue(o);
        }

        public static void Remove(object o)
        {
            lock (Map)
            {
                var pair = Map.ToList().FirstOrDefault(item => o == item.Value);
                if (pair.Value != null)
                    Map.Remove(pair.Key);
            }
        }
    }
}
